package com.zsyh.jiujiu.home;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zyh.toolslib.base.BaseActivity;
import com.example.zyh.toolslib.widget.TitleBar;
import com.google.gson.Gson;
import com.lwb.retrofit.RetrofitTask;
import com.zsyh.jiujiu.Api;
import com.zsyh.jiujiu.R;
import com.zsyh.jiujiu.adepter.ClassErJIAdepter;
import com.zsyh.jiujiu.adepter.ClassOneAdepter;
import com.zsyh.jiujiu.adepter.ClassThreeAdepter;
import com.zsyh.jiujiu.adepter.CommonAdapter;
import com.zsyh.jiujiu.bean.ClassOneBean;
import com.zsyh.jiujiu.net.MyObserver;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @ProjectName: livvehelp
 * @Package: com.zsyh.lsb.ui
 * @ClassName:
 * @Description: java类作用描述
 * @Author: zsyh
 * @CreateDate: 2020/9/7 11:59
 * @UpdateUser: liangwanbo
 * @UpdateDate: 2020/9/7 11:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ClassFicationActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.onelist)
    RecyclerView onelist;
    @BindView(R.id.twolist)
    RecyclerView twolist;
    @BindView(R.id.classlist)
    RecyclerView classlist;
    private ClassOneAdepter classOneAdepter;
    private List<Integer> listonein=new ArrayList<>();

    private ClassErJIAdepter classErJIAdepter;
    private List<Integer> listtwoin=new ArrayList<>();

    private ClassThreeAdepter classThreeAdepter;
    private List<Integer> listthreein=new ArrayList<>();
    private String yijiname,ername,sanname,yiid,erid,sanid;
    @Override
    protected int initLayout() {
        return R.layout.activity_fenlei;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        Classfenlei("");
    }

    @Override
    protected void initEvent() {
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thisActivity.finish();
            }
        });
        titleBar.setRightLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                thisActivity.finish();
            }
        });
    }

    /**
     * yiji
     */
    public void Classfenlei(String pid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getGoodsClass(pid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("tixian",JsonStr+"");
                        Gson gson=new Gson();
                        ClassOneBean sucessBean=gson.fromJson(JsonStr, ClassOneBean.class);
                        for (int i = 0; i < sucessBean.getData().size(); i++) {
                            listonein.add(0);
                        }
                        yijiname=sucessBean.getData().get(0).getClassName();
                        yiid=sucessBean.getData().get(0).getId();
                        erji(sucessBean.getData().get(0).getId());
                        classOneAdepter = new ClassOneAdepter(thisActivity, R.layout.item_yiji, sucessBean.getData(), listonein);
                        onelist.setLayoutManager(new LinearLayoutManager(thisActivity));
                        onelist.setAdapter(classOneAdepter);
                        classOneAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                setpostion1(listonein, position);
                                classOneAdepter.notifyDataSetChanged();
                                yijiname=sucessBean.getData().get(position).getClassName();
                                yiid=sucessBean.getData().get(position).getId();
                                erji(sucessBean.getData().get(position).getId());
                            }
                        });

                    }
                });
    }

    /**
     * erji
     * @param
     * @param
     */

    public void erji(String pid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getGoodsClass(pid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("erjishuju",JsonStr+"");
                        Gson gson=new Gson();
                        ClassOneBean sucessBean=gson.fromJson(JsonStr, ClassOneBean.class);
                        if(listtwoin!=null){
                            listtwoin.clear();
                        }
                        for (int i = 0; i < sucessBean.getData().size(); i++) {
                            listtwoin.add(0);
                        }
                        classErJIAdepter = new ClassErJIAdepter(thisActivity, R.layout.item_erji, sucessBean.getData(), listtwoin);
                        twolist.setLayoutManager(new LinearLayoutManager(thisActivity));
                        twolist.setAdapter(classErJIAdepter);
                        ername=sucessBean.getData().get(0).getClassName();
                        erid=sucessBean.getData().get(0).getId();
                        ThreeClass(sucessBean.getData().get(0).getId());
                        classErJIAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                setpostion12(listtwoin, position);
                                classErJIAdepter.notifyDataSetChanged();
                                ername=sucessBean.getData().get(position).getClassName();
                                erid=sucessBean.getData().get(position).getId();
                                ThreeClass(sucessBean.getData().get(position).getId());
                            }
                        });

                    }
                });
    }

    /**
     * 三级
     * @param
     * @param
     */

    public void ThreeClass(String pid){
        RetrofitTask.getInstance()
                .createApi(Api.class)
                .getGoodsClass(pid)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new MyObserver(this,false) {
                    @Override
                    protected void onError(String errorMsg) {

                    }

                    @Override
                    protected void onSuccess(String JsonStr) {
                        Log.e("sanjishuju",JsonStr+"");
                        Gson gson=new Gson();
                        ClassOneBean sucessBean=gson.fromJson(JsonStr, ClassOneBean.class);
                        if(listthreein!=null){
                            listthreein.clear();
                        }
                        for (int i = 0; i < sucessBean.getData().size(); i++) {
                            listthreein.add(0);
                        }
                        classThreeAdepter = new ClassThreeAdepter(thisActivity, R.layout.item_sanji, sucessBean.getData(), listthreein);
                        classlist.setLayoutManager(new LinearLayoutManager(thisActivity));
                        classlist.setAdapter(classThreeAdepter);
                        classThreeAdepter.notifyDataSetChanged();
                        classThreeAdepter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClick(View view, int position) {
                                sanname=sucessBean.getData().get(position).getClassName();
                                sanid=sucessBean.getData().get(position).getId();
                                Intent intent = new Intent();
                                intent.putExtra("yijiname", yijiname);
                                intent.putExtra("erjiname",ername);
                                intent.putExtra("sanjiname", sanname);
                                intent.putExtra("yiid",yiid);
                                intent.putExtra("erid", erid);
                                intent.putExtra("sanid", sanid);
                                setResult(2, intent);
                                ClassFicationActivity.this.finish();
                            }
                        });

                    }
                });
    }




    public void setpostion1(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }

    public void setpostion12(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }


    public void setpostion13(List<Integer> list, int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.set(postion, 1);
            } else {
                list.set(i, 0);
            }
        }

    }


}
